﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using DataAccess;
using DevExpress.XtraEditors;
using System.Net.Mail;
using NLog;

namespace QuanLyKhachSan
{
    public partial class FormResetMatKhau : XtraForm
    {
        protected static Logger _logger = LogManager.GetCurrentClassLogger();
        public bool IsInserted = false;
        private BaoMatManager bmManager = new BaoMatManager();
        private TaiKhoanManager manager = new TaiKhoanManager();
        private List<TaiKhoan> taikhoan = new List<TaiKhoan>();
        TaiKhoan tkdangreset;
        public Commons common = new Commons();

        public FormResetMatKhau()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            _logger.Info("Chọn thao tác reset mật khẩu.");
            if (txtEmail.Text == "")
                XtraMessageBox.Show("Chưa nhập email");
            else
            {
                IEnumerable<TaiKhoan> inum = manager.LayDanhSach().Result.Where
                    (tk => tk.EmailDangKy.Equals(txtEmail.Text));
                taikhoan = inum.ToList();
                if (taikhoan.Count == 0)
                {
                    XtraMessageBox.Show("Email này không tồn tại");
                    txtEmail.ResetText();
                }
                else
                {
                    tkdangreset = taikhoan[0];
                    tkdangreset.PasswordTamThoi = RandomString(6);
                    tkdangreset.TrangThai = "Đang đổi mật khẩu";
                    ActionResult actionResult;
                    actionResult = bmManager.CapNhatReset(tkdangreset);
                    SendEmail(tkdangreset.Username, tkdangreset.PasswordTamThoi, tkdangreset.EmailDangKy);
                    if (!actionResult.Sucess())
                        MessageBox.Show(actionResult.GetErrorMessage());
                    else
                    {
                        IsInserted = true;
                        Close();
                    }
                }
            }
        }

        private void SendEmail(string user, string password, string email)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("webofpumbaa1@gmail.com");
                mail.To.Add(email);
                mail.Subject = "Mật khẩu đăng nhập mới trong phần mềm QLKhachSan của bạn";
                mail.Body = "Bạn đã được reset mật khẩu mới cho user [" + user + "] là : " + password;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("webofpumbaa1@gmail.com", "kiet071193");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("Đã gửi mail");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private string RandomString(int size)
        {
            StringBuilder sb = new StringBuilder();
            char c;
            Random rand = new Random();
            for (int i = 0; i < size; i++)
            {
                c = Convert.ToChar(Convert.ToInt32(rand.Next(65, 87)));
                sb.Append(c);
            }
            return sb.ToString();
        }

        private void FormResetMatKhau_Load(object sender, EventArgs e)
        {
            txtEmail.Text = "";
        }

    }
}
